package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @program: yygh_parent
 * @author: Mr.Zhuo
 * @create: 2022-03-12 11:13
 **/
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void saveDepartment(Map<String, Object> paramOB) {
        // 然后json转换数据为对象
        String jsonString = JSONObject.toJSONString(paramOB);
        Department department = JSONObject.parseObject(jsonString, Department.class);

        departmentRepository.save(department);
    }

    @Override
    public Page<Department> findDepartmentByQueryAndLimit(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        // 进行数据排序
        Sort sort = Sort.by(Sort.Direction.ASC, "createTime");

        // 进行分页数据处理
        PageRequest pageRequest = PageRequest.of(page - 1, limit, sort);

        // 查询条件
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        // 进行数据模糊查询
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        Example<Department> example = Example.of(department, matcher);

        // 然后进行查询
        Page<Department> all = departmentRepository.findAll(example, pageRequest);
        return all;
    }

    @Override
    public void remove(Map<String, Object> paramOB) {
        // 数据转换
        String hoscode = (String) paramOB.get("hoscode");
        String depcode = (String) paramOB.get("depcode");

        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (null != department) {
            departmentRepository.deleteById(department.getId());
        }
    }

    //科室
    @Override
    public Department findDepartmentByHoscodeDepcode(String hoscode, String depcode) {
        Department hoscodeAndDepcode = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return hoscodeAndDepcode;
    }

    //根据医院编号，查询医院所有科室列表
    @Override
    public List<DepartmentVo> getDeptList(String hoscode) {
        // 模拟返回的数据
        List<DepartmentVo> departmentVoList = new ArrayList<>();

        // 设置条件查询
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQuery);
        // 查出所有相关的数据
        List<Department> departmentList = departmentRepository.findAll(example);

        // 进行数据转换
        Map<String, List<Department>> listMap
                = departmentList.stream().collect(Collectors.groupingBy(Department::getBigname));

        // 上述数据就是以大科室名字为组：集合里面都是小科室
        Set<String> keySet = listMap.keySet();
        for (String bigName : keySet) {
            DepartmentVo departmentVo = new DepartmentVo();
            // 大科室数据传输
            departmentVo.setDepname(bigName);
            String bigcode = listMap.get(bigName).get(0).getBigcode();
            departmentVo.setDepcode(bigcode);


            // 小科室数据传输
            List<DepartmentVo> children = new ArrayList<>();
            List<Department> departments = listMap.get(bigName);
            departments.stream().forEach(item -> {
                DepartmentVo vo = new DepartmentVo();
                BeanUtils.copyProperties(item, vo);

                children.add(vo);
            });
            departmentVo.setChildren(children);
            // 最终把数据填写到返回数据中
            departmentVoList.add(departmentVo);
        }

        return departmentVoList;
    }
}
